const {
  imgConfig, config
} = require('../../utils/config.js')
const app = getApp()
Component({
  
    properties: {
      clockDatas: {
        type: Array
      }
    },
    data: {
      showMore: true,
      weekData: ['SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT'],
      miniContent: [
        ///monthStatus:0-上月日期，1-本月日期，2-下月日期，isSignIn是否已签到，isToday当前日期是否是今天，leaveDate是是否请假
        { id: 1, title: "26", monthStatus: 1, isSignIn: false, isToday: false,leaveDate: false }
      ],
      largeContent: [],
      today: "",
      leaveList: [],
      currentYear: "",
      currentMonth: "",
      currentDate: "",
      lastSwiper: 0
    },
    observers: {
      'clockDatas': function(data){
        console.log("数据更新");
        let changeDate = this.data.currentYear + "-" + this.data.currentMonth + "-1";
        // 延迟调用setTimeout 处理异步获取年，月数据
        // getAttendanceCount获取请假天数-leaveList 请假天数list
          this.getAttendanceCount()
          setTimeout(() => {
          this.getLargeDate(changeDate);
          }, 300); 
      }
    },
    ready: function () {
      this.initCalendar();
    },
    methods: {
      contentToggle(){
        this.setData({
        showMore: !this.data.showMore
        })
      },
     /**
     * 获取请假申请列表
     */
    getAttendanceCount(){
      var that = this
          wx.request({
            url: `${config}/wechat/wxAttendance/getAttendanceList`,
            method:'get', 
            data:{
              stuId: app.globalData.userInfo.id,
              currentMonth:this.data.currentMonth,
              currentYear:this.data.currentYear,
            },    
            success(resp){
              console.log(resp.data);
                that.setData({
                  leaveList:resp.data.countMonth
              })
            }
          })
      
    },
      //初始化日历
      initCalendar(){
        let dateObject = new Date(),
          year = dateObject.getFullYear(),
          month = dateObject.getMonth() + 1,
          date = dateObject.getDate(),
          today = year + '-' + this.zero(month) + '-' + this.zero(date);
   
        this.setData({
          today: today,
          currentYear: year,
          currentMonth: month,
          currentDate: date
        });
   
        this.getMiniDate();
        this.getLargeDate();
      },
   
      // 获取小日期
      getMiniDate(){
        let dateObject = new Date();
        let toMonth = dateObject.getMonth() + 1;
        let toDay = dateObject.getDate();
        let toWeek = dateObject.getDay();
        dateObject.setDate(dateObject.getDate() - toWeek);
        let miniData = [];
        for(let i=0;i<7;i++){
          let currentDate = this.getFormatDate(dateObject);
          let clockDate = this.data.clockDatas.filter((item) => item.date==currentDate.date);
          let leaveList = [];
          if (this.data.leaveList.length != 0) {   
            for (let i = 0; i < this.data.leaveList.length; i++) {
            leaveList.push(this.data.leaveList[i]['applyDate'] )
          }  
          }
          let currentObj = {
            title: currentDate.day,
            monthStatus: (currentDate.month > toMonth ? 2 : (currentDate.month < toMonth ? 0 : 1)),
            isSignIn: clockDate.length>0?true:false,
            isToday: (currentDate.day==toDay ? true : false),
            leaveDate:(leaveList.includes(currentDate.date))     
          }
          miniData.push(currentObj);
          dateObject.setDate(dateObject.getDate()+1);
        }
   
        //console.log("小日期", miniData);
        this.setData({
          miniContent: miniData
        });
      },
     

   
      // 获取大的日期
      getLargeDate(dates){
        let dateObject = dates ? new Date(dates) : new Date();
        let toMonth = dateObject.getMonth() + 1;
        let toDay = dateObject.getDate();
        let toWeek = new Date(dateObject.getFullYear(), toMonth - 1, 1).getDay(); // 获取本月一号的星期
        let toDates = 42;
        dateObject.setDate(dateObject.getDate() - (toWeek + toDay - 1));
        let largeData = [];
        let leaveList = [];
        if (this.data.leaveList.length != 0) {   
          for (let i = 0; i < this.data.leaveList.length; i++) {
          //  leaveList.push(this.data.leaveList[i]['beginDate'] )
          if (this.data.leaveList[i].beginDate !=this.data.leaveList[i].endDate) {
            let a = this.data.leaveList[i]['endDate'].substring(8)-this.data.leaveList[i]['beginDate'].substring(8)
            for (let j = 0; j < a; j++) {
              // 定义时间
              var diyDate = new Date(this.data.leaveList[i]['beginDate'])
              //加1天
              let date = new Date(diyDate.setDate(diyDate.getDate() + j));
              // 拼接日期 yyyy-MM-dd 注意月份要 + 1
              let begendList = date.getFullYear() + "-" + ((date.getMonth() + 1) > 9 ? date.getMonth() + 1 : "0" + (date.getMonth() + 1)) + "-" + (date.getDate() > 9 ? date.getDate() : "0" + date.getDate());
             leaveList.push(begendList)
            }
          }else{
            leaveList.push(this.data.leaveList[i]['applyDate'])
          }
        }  
        }
        for (let i = 0; i < toDates; i++) {
          let currentDate = this.getFormatDate(dateObject);
          let clockDate = this.data.clockDatas.filter((item) => item.date==currentDate.date);
          if (leaveList != []) {
            let currentObj = {
              title: currentDate.day,
              monthStatus: (currentDate.month > toMonth ? 2 : (currentDate.month < toMonth ? 0 : 1)),
              isSignIn: clockDate.length>0?true:false,
              isToday: (currentDate.date == this.data.today ? true : false),
              leaveDate:(leaveList.includes(currentDate.date)),
            } 
          largeData.push(currentObj);
          dateObject.setDate(dateObject.getDate() + 1);
          } 
        }
        //console.log("大日期", largeData);
        this.setData({
          largeContent: largeData
        });
      },
   
      getFormatDate(date){
        let year = date.getFullYear(),
          month = date.getMonth() + 1,
          day = date.getDate();
        let currentDate = year + "-" + this.zero(month) + "-" + this.zero(day);
        return {
          year: year,
          month: month,
          day: day,
          date: currentDate
        }
      },
   
      // 滑块改变事件
      swiperChange(e){
        let current = e.detail.current;
        let lastSwiper = this.data.lastSwiper;
        let status = current - lastSwiper;
        if(status == -1 || status == 2){
          //console.log("右滑");
          this.setCurrentDate(0); //月份减一
        }else if(status == 1 || status == -2){
          //console.log("左滑");
          this.setCurrentDate(1); //月份加一
        }else{
          console.log("其他");
        }
   
        let changeDate = this.data.currentYear + "-" + this.data.currentMonth + "-1";
        //this.getLargeDate(changeDate);
        this.triggerEvent('changeMonth',{year: this.data.currentYear, month: this.data.currentMonth});
   
   
        this.setData({
          lastSwiper: current
        })
      },
   
      // 根据传入的参数加减月份
      setCurrentDate(type){
        let currentYear = Number(this.data.currentYear);
        let currentMonth = Number(this.data.currentMonth);
        if(type){
          if(currentMonth==12){
            currentYear = currentYear + 1;
            currentMonth = 1;
          }else{
            currentMonth = currentMonth + 1;
          }
        }else{
          if(currentMonth == 1){
            currentYear = currentYear - 1;
            currentMonth = 12;
          }else{
            currentMonth = currentMonth - 1;
          }
        }
   
        this.setData({
          currentYear: currentYear,
          currentMonth: currentMonth
        })
   
      },
   
      //补全0
      zero: function (i) {
        return i >= 10 ? i : '0' + i;
      },


    }
    
  })
